package indi.zhifa.recipe.bailan.busy.dao.repository;

import indi.zhifa.recipe.bailan.busy.entity.po.OrmTestEntity;

import indi.zhifa.recipe.bailan.busy.entity.po.QOrmTestEntity;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.querydsl.binding.QuerydslBinderCustomizer;
import org.springframework.data.querydsl.binding.QuerydslBindings;
import org.springframework.data.repository.CrudRepository;

import java.util.Iterator;
import java.util.Optional;

public interface OrmTestReposity extends CrudRepository<OrmTestEntity,Long>, QuerydslPredicateExecutor<OrmTestEntity>, QuerydslBinderCustomizer<QOrmTestEntity> {

    @Override
    default void customize(QuerydslBindings bindings, QOrmTestEntity pQOrmTestEntity){
        // name 使用like
        bindings.bind(pQOrmTestEntity.name).first((path,value)->path.contains(value));
        bindings.bind(pQOrmTestEntity.appName).first((path,value)->path.contains(value));
        bindings.bind(pQOrmTestEntity.moduleName).first((path,value)->path.contains(value));
        bindings.bind(pQOrmTestEntity.description).first((path,value)->path.contains(value));
        bindings.bind(pQOrmTestEntity.value).all((path,value) -> {
            Iterator<? extends Integer> it = value.iterator();
            Integer from = it.next();
            if (value.size() >= 2) {
                Integer to = it.next();
                return Optional.of(path.between(from, to)); // between
            } else {
                return Optional.of(path.goe(from)); // greater or equal
            }
        });
    }

}
